import requests
from lxml import html
from bs4 import BeautifulSoup
import re
import xlwt


class MeiTuanCity(object):
    def __init__(self):
        # self.lagou_session = requests.session()
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/53",
        }
        self.city_list = ""
        self.f = xlwt.Workbook()
        self.sheet1 = self.f.add_sheet(u'sheet1', cell_overwrite_ok=True)

    def getData(self):
        city_list = self.handle_request_city()
        # list_data = []
        for item in city_list:
            hair_link = 'https:'+item['href']+'/jiankangliren/c74/'
            hair_list = self.handle_request_list(url=hair_link)
            print(hair_list)
            

    def handle_request_city(self):
        city_url = "https://www.meituan.com/changecity/"
        responses = requests.get(url=city_url, headers=self.headers)
        resp = responses.content.decode('utf-8')
        etree = html.etree
        htmls = etree.HTML(resp)
        print(htmls)
        city_list = htmls.xpath('//div[@class="city-area"]//span/a')
        citys = []
        print('你好')
        for item in city_list:
            items = {}
            items['name'] = "".join(item.xpath('.//text()'))
            items['href'] = "".join(item.xpath('./@href'))
            citys.append(items)
        return citys
    # 获取数据

    def handle_request_list(self, url):
        response = requests.get(url=url, headers=self.headers)
        resp = response.content.decode('utf-8')
        etree = html.etree
        htmls = etree.HTML(resp)
        print(htmls)
        lists = []
        hair_list = htmls.xpath(
            '//div[@class="abstract-item clearfix"]//div[@class="abstract-desc"]')
        # print(hair_list)
        for index, item in enumerate(hair_list):
            items = {}
            items['title'] = "".join(item.xpath('.//a/text()'))
            items['address'] = "".join(item.xpath(
                './/div[@class="item-site-info clearfix"]//span[2]/text()'))
            lists.append(items)
            self.sheet1.write(index,0,items['title'])
            self.sheet1.write(index,1,items['address'])
        
        self.f.save('sheet1.xls')
        return lists


if __name__ == '__main__':
    city = MeiTuanCity()
    city.getData()
